package com.zjdiepu.www.biz.fk;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.fk.FkPreAuditInfoMapper;
import com.zjdiepu.www.ibiz.fk.IFkPreAuditInfoService;
import com.zjdiepu.www.model.fk.dto.FkPreAuditInfo;
import com.zjdiepu.www.model.fk.ext.FkPreAuditInfoExt;

/**
 * fk_预审信息表Service层操作实现<br/>
 * @author zhangpp<br/>
 * @time 2017年08月07日 11时59分<br/>
 * @since 1.0<br/>
 */
@Service
public class FkPreAuditInfoServiceImpl extends BaseService implements IFkPreAuditInfoService {
	public static final Logger logger = LoggerFactory.getLogger(FkPreAuditInfoServiceImpl.class);

	@Autowired
	private FkPreAuditInfoMapper fkPreAuditInfoMapper;
	
	@Override
	public void saveUpdateAndDelete(FkPreAuditInfo fkPreAuditInfo, OperateType operType) {
		switch (operType) {
		case add:
			fkPreAuditInfo.initInsertData(fkPreAuditInfo.getModifier(), fkPreAuditInfo.getModifierName());
			fkPreAuditInfoMapper.insert(fkPreAuditInfo);
			break;
		case edit:
			fkPreAuditInfo.initUpdateData(fkPreAuditInfo.getModifier(), fkPreAuditInfo.getModifierName());
			// 影响的记录数
			int updateCount = fkPreAuditInfoMapper.updateByPrimaryKeySelective(fkPreAuditInfo);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			fkPreAuditInfo.initDeleteData(fkPreAuditInfo.getModifier(), fkPreAuditInfo.getModifierName());
			// 影响的记录数
			int deleteCount = fkPreAuditInfoMapper.updateByPrimaryKeySelective(fkPreAuditInfo);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<FkPreAuditInfo> selectByExt(FkPreAuditInfoExt fkPreAuditInfoExt) {
		return fkPreAuditInfoMapper.selectByExt(fkPreAuditInfoExt);
	}
	
	@Override
	public int countByExt(FkPreAuditInfoExt fkPreAuditInfoExt){
		return fkPreAuditInfoMapper.countByExt(fkPreAuditInfoExt);
	}
	
	@Override
	public FkPreAuditInfo selectById(String id) {
		return fkPreAuditInfoMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			fkPreAuditInfoMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(FkPreAuditInfo fkPreAuditInfo) {
		return fkPreAuditInfoMapper.countRefined(fkPreAuditInfo);
	}

	@Override
	public FkPreAuditInfo selectRefined(FkPreAuditInfo fkPreAuditInfo) {
		return fkPreAuditInfoMapper.selectRefined(fkPreAuditInfo);
	}
	
	@Override
	public int deletedRefined(FkPreAuditInfo fkPreAuditInfo) {
		return fkPreAuditInfoMapper.deletedRefined(fkPreAuditInfo);
	}

	@Override
	public boolean repeat(FkPreAuditInfo fkPreAuditInfo) {
		int count = countRefined(fkPreAuditInfo);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(fkPreAuditInfo.getId())) {
			return true;
		}
		FkPreAuditInfo dbItem = selectRefined(fkPreAuditInfo);
		if(null == dbItem) {
			return false;
		}
		return !fkPreAuditInfo.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return fkPreAuditInfoMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<FkPreAuditInfo> fkPreAuditInfos) {
		return fkPreAuditInfoMapper.insertBatch(fkPreAuditInfos);
	}
}
